#! /bin/bash

#mencoder ${file} -oac mp3lame -lameopts aq=3:abr:br=128 -srate 44100 -ovc frameno -o ${file}.mp3 2>/dev/null

rtype="mp3"
file=
robject=
starttime=
endtime=


while getopts t:hf:o:s:e: OPTION 2>/dev/null
do
	case $OPTION in
		h) echo "Usage:`basename $0` [-h] [-t type] [-f file] [-o objectfile] [-s starttime] [-e endtime]"
			exit 0
			;;
		t) rtype=$OPTARG
		   if [ "${rtype}" != "mp3" -o "${rtype}" != "avi" ];then
				echo "-t arguments could only be mp3 or avi"
				exit 1
			fi
			;;
		o) robject=$OPTARG
			;;
		f) file=$OPTARG
			;;
		s) starttime=$OPTARG
			;;
		e) endtime=$OPTARG
			;;
		*) echo 'try -h for help'
		   exit 1
			;;	
	esac

done

if [ -z $file ];then 
	echo "file not set"
	exit 1
fi

if [ -z $robject ];then 
	echo "object file not set"
	exit 1
fi

if [ -z $starttime ];then 
	echo "start time not set"
	exit 1
fi

if [ -z $endtime ];then 
	echo "end time not set"
	exit 1
fi

if ! which mencoder 2>/dev/null;then
	echo "mencoder is not in your system"
	exit 1
fi

if [ "${rtype}" = "mp3" ];then
	mencoder -ss ${starttime} -oac mp3lame -lameopts aq=3:abr:br=128 -srate 44100 -ovc frameno -entpos ${endtime} ${file} -o ${robject}
elif [ "${rtype}" = "avi" ];then
	mencoder -ss ${starttime} -oac mp3lame -lameopts aq=3:abr:br=128 -srate 44100 -ovc avc -entpos ${endtime} ${file} -o ${robject}
fi
